package com.fyy.netty.chapter02.c2;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * @Author fyy
 * @Description
 * @Date 2021/4/27 10:13
 */
@Slf4j
public class NettyFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();

        Future<Integer> future = eventLoop.submit(() -> {
            log.info("等待计算结果... ...");
            Thread.sleep(1000);
            return 50;
        });

        //同步获取
//        log.info("获取计算结果:{}",future.get());
//        log.info("获取计算结果:{}",future.sync().getNow());
        //异步获取
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.info("获取计算结果:{}",future.getNow());
            }
        });

    }

}
